package com.maguangwu.controller;

import com.maguangwu.constant.MessageConstant;
import com.maguangwu.entity.PageResult;
import com.maguangwu.entity.QueryPageBean;
import com.maguangwu.entity.Result;
import com.maguangwu.exception.MyException;
import com.maguangwu.pojo.CheckItem;
import com.maguangwu.server.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mgw
 * @create 2021/10/20 23:06
 */
@RestController
@RequestMapping("checkitem")
public class CheckitemController {
    @Autowired
    private CheckitemService checkitemService;

    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        checkitemService.add(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/findAll")
    public Result findAll() {
      List<CheckItem> checks= checkitemService.findAll();
        return new Result(true, MessageConstant.SELECTED_DATE_CANNOT_ORDER,checks);
    }
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
      PageResult<CheckItem> pageResult= checkitemService.findPage(queryPageBean);
      return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS,pageResult);
    }
    @GetMapping("/update/{id}")
    public Result findById(@PathVariable Integer id){
      CheckItem checkItem= checkitemService.findById(id);
      return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS,checkItem);
    }
    @PostMapping("/update")
    public Result UpdateById(@RequestBody CheckItem checkItem){
        if (checkItem.getName().isEmpty()||"".equals(checkItem.getName())){
           throw  new MyException("项目名称不能为空");
        }
        checkitemService.updateById(checkItem);
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    @GetMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        checkitemService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
}
